package com.framework.admin.controller;

import com.framework.admin.domain.param.ModuleTableFieldParam;
import com.framework.admin.model.ModuleTableField;
import com.framework.admin.service.IModuleTableFieldService;
import com.framework.common.domain.ResultData;
import com.framework.common.exception.BusinessException;
import com.framework.common.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @description: 模块数据表字段 Service
 * @author: lizurong
 * @version: 1.0
 * @date: 2021-03-24 11:18:24
 */
@RestController
@RequestMapping("/modeltablefield")
@Api(value = "ModelTableField", tags = "模块数据表字段")
public class ModuleTableFieldController {
    @Autowired
    private IModuleTableFieldService baseService;


    @ResponseBody
    @ApiOperation(value = "新增")
    @PostMapping(value = "/add")
    public ResultData add(@RequestBody ModuleTableField param) {
        if (StringUtils.isEmpty(param.getModuleFormId())) {
            throw new BusinessException("模块id参数不能为空!");
        }
        baseService.save(param);
        return ResultData.success();
    }

    @ResponseBody
    @ApiOperation(value = "删除")
    @PostMapping(value = "/del")
    public ResultData delete(@RequestBody List<String> ids) {
        baseService.delete(ids);
        return ResultData.success();
    }

    @ResponseBody
    @ApiOperation(value = "编辑")
    @PostMapping(value = "/edit")
    public ResultData edit(@RequestBody ModuleTableField param) {
        baseService.update(param);
        return ResultData.success();
    }

    @ApiOperation("获取分页")
    @PostMapping(value = "/listPage")
    public ResultData listPage(@RequestBody(required = false) ModuleTableFieldParam param) {
        PageRequest pageRequest = PageRequest.of(param.getPageNo(), param.getPageSize());
        Page<ModuleTableField> users = baseService.listPage(param, pageRequest);
        return ResultData.data(users);
    }

    @ApiOperation("修改表名、表描述")
    @PostMapping(value = "/editTableNameAndTableDesc")
    public ResultData editTableNameAndTableDesc(@RequestParam String moduleFormId, @RequestParam String tableName, @RequestParam String tableDesc) {
        baseService.editTableNameAndTableDesc(moduleFormId, tableName, tableDesc);
        return ResultData.success();
    }

    @ApiOperation("生成表")
    @PostMapping(value = "/genDbTable")
    public ResultData genDbTable(@RequestParam String moduleFormId, @RequestParam String tableName, @RequestParam String tableDesc) {
        baseService.genDbTable(moduleFormId, tableName, tableDesc);
        return ResultData.success();
    }

    @ApiOperation("删除表")
    @GetMapping(value = "/deleteTable")
    public ResultData deleteTable(@RequestParam String moduleFormId) {
        baseService.deleteTable(moduleFormId);
        return ResultData.success();
    }

}
